﻿

using System;
using System.Text;
using System.Collections.Generic;

namespace Common
{
    /// <summary>
    /// 针对System.String的辅助类
    /// </summary>
    public static class StringHelper
    {
        #region Basic Operate
        /// <summary>
        /// 获取字节数(AsciiLength)
        /// </summary>
        /// <param name="s"></param>
        /// <returns></returns>
        public static int GetBytesCount( string s )
        {
            return System.Text.Encoding.ASCII.GetByteCount( s );
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="source"></param>
        /// <param name="startIndex"></param>
        /// <param name="length"></param>
        /// <returns></returns>
        public static string SubString( /*this*/ string source, int startIndex, int length )
        {
            return source.Substring( startIndex, length );
            /*
            if (startIndex > source.Length)
            {
                return string.Empty;
            }

            if (startIndex + length > source.Length)
            {
                return source.Substring( startIndex, startIndex );
            }
            else
            {
                return source.Substring( startIndex, length );
            }*/
        }


        /// <summary>
        ///  返回分隔符字符串信息
        /// </summary>
        /// <param name="source">源字符串(123-232-232)</param>
        /// <param name="spliter">分隔符(-)</param>
        /// <returns>字符数组(string[]{123,232,232})</returns>
        public static string[] SpliteStrings(string source, string spliter)
        {
            if (string.IsNullOrEmpty(source))
                return new string[] { };


            return source.Split(spliter.ToCharArray());
        }


        /// <summary>
        /// 返回分隔符字符串信息(排除空字符)
        /// </summary>
        /// <param name="source">源字符串</param>
        /// <param name="spliter">分隔符</param>
        /// <returns>字符数组</returns>
        public static string[] SpliteStringsClearEmpty(string source, string spliter)
        {
            var parts = SpliteStrings(source, spliter);

            if (parts.Length > 0)
            {
                var list = new List<string>();

                foreach (var part in parts)
                {
                    if (!string.IsNullOrEmpty(part))
                        list.Add(part);
                }

                parts = list.ToArray();
            }

            return parts;
        }
        #endregion

        #region Xml Serialization Character Check
        /// <summary>
        /// 是否含有ASCII码小于等于31的字符
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public static bool HasXmlEspecialChar( string input )
        {
            if (string.IsNullOrEmpty( input ))
            {
                return false;
            }

            for (int i = 0; i < input.Length; i++)
            {
                int val = input[i];
                if (val <= 31 && val != 10 && val != 13)
                {
                    return true;
                }
            }
            return false;
        }

        /// <summary>
        /// 去除ASCII码小于等于31的字符
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public static string RemoveXmlEspecialChar( string input )
        {
            if (string.IsNullOrEmpty( input ))
            {
                return input;
            }

            StringBuilder buffer = new StringBuilder();
            for (int i = 0; i < input.Length; i++)
            {
                char c = input[i];
                int val = (int)c;
                if (val > 31 && val != 10 && val != 13)
                {
                    buffer.Append( c );
                }
            }
            return buffer.ToString();
        }
        #endregion

        #region SBC/DBC Convert
        /// <summary>
        /// 全角转半角
        /// SBC -> DBC
        /// 
        /// 全角空格为12288，半角空格为32
        /// 其他字符半角(33-126)与全角(65281-65374)的对应关系是：均相差65248
        /// </summary>
        /// <param name="input">任意字符串</param>
        /// <returns>半角字符串</returns>
        public static string ToDBC( string input )
        {
            if (string.IsNullOrEmpty( input ))
            {
                return input;
            }

            char[] c = input.ToCharArray();
            for (int i = 0; i < c.Length; i++)
            {
                if (c[i] == 12288)
                {
                    c[i] = (char)32;
                    continue;
                }
                if (c[i] > 65280 && c[i] < 65375)
                {
                    c[i] = (char)(c[i] - 65248);
                }
            }
            return new string( c );
        }

        /// <summary>
        /// 半角转全角
        /// DBC -> SBC
        /// 
        /// 全角空格为12288，半角空格为32
        /// 其他字符半角(33-126)与全角(65281-65374)的对应关系是：均相差65248
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public static string ToSBC( string input )
        {
            if (string.IsNullOrEmpty( input ))
            {
                return input;
            }

            //半角转全角：
            char[] c = input.ToCharArray();
            for (int i = 0; i < c.Length; i++)
            {
                if (c[i] == 32)
                {
                    c[i] = (char)12288;
                    continue;
                }
                if (c[i] < 127)
                {
                    c[i] = (char)(c[i] + 65248);
                }
            }
            return new string( c );
        }
        #endregion

        #region Type Convert
        public static T Convert<T>( string value ) where T : IConvertible
        {
            if (value == null)
            {
                return default( T );
            }
            else
            {
                return (T)System.Convert.ChangeType( value, typeof( T ) );
            }
        }

        public static T Convert<T>( string value, T defaultValue ) where T : IConvertible
        {
            try
            {
                if ( string.IsNullOrEmpty( value ) )
                {
                    return defaultValue;
                }
                else
                {
                    return (T)System.Convert.ChangeType( value, typeof( T ) );
                }
            }
            catch
            {
                return defaultValue;
            }
        }

        /// <summary>
        /// string To Enum
        /// </summary>
        /// <param name="inputText">The input text.</param>
        /// <param name="defaultValue">The default value.</param>
        /// <returns></returns>
        public static Enum ConvertEnum(string inputText, Enum defaultValue)
        {
            if (string.IsNullOrEmpty(inputText))
            {
                return defaultValue;
            }

            try
            {
                return (Enum)Enum.Parse(defaultValue.GetType(), inputText, true);
            }
            catch
            {
                return defaultValue;
            }
        }
        #endregion
    }
}
